package com.detrum.detrumdx.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.detrum.base.BaseStatic;
import com.detrum.bean.FileBean;
import com.detrum.detrumdx.R;
import com.detrum.swipe.BaseRecyclerAdapter;
import com.detrum.utils.LogUtils;

import java.util.ArrayList;


/**
 * Created by abc on 2018/1/4.
 */

public class LocalRefreshAdapter extends BaseRecyclerAdapter<LocalRefreshAdapter.LocalHolder,FileBean> {
    private final String TAG = "LocalRefreshAdapter";
    private Context context;
    private ArrayList<FileBean> list_bean;
    private boolean isEdit;

    public LocalRefreshAdapter(ArrayList<FileBean> mDataList, Context context) {
        super(mDataList);
        this.context = context;
        list_bean = mDataList;
    }

    @Override
    public int getItemCount() {
        return list_bean==null?0:list_bean.size();
    }

    public void setEdit(boolean isEdit){
        this.isEdit = isEdit;
    }

    @Override
    public LocalHolder createViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.nfsitemview, parent, false);
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) itemView.getLayoutParams();
        params.width = BaseStatic.SCREEN_WIDTH / 2;
        params.height = BaseStatic.SCREEN_WIDTH / 2;
        itemView.setLayoutParams(params);
        return new LocalHolder(itemView);
    }

    @Override
    public void onBindViewHolder(LocalHolder holder, int position, FileBean data) {
        holder.position = position;
        String fileName = null;
        fileName = data.getFileName();
        boolean isSelect = data.isSelect();
        Log.i(TAG, position + "----===----" + fileName);
        if (isEdit) {
            holder.select_iv.setVisibility(View.VISIBLE);
            if (isSelect) {
                holder.select_iv.setImageResource(R.mipmap.select);
            } else {
                holder.select_iv.setImageResource(R.mipmap.noselect);
            }
        } else {
            holder.select_iv.setVisibility(View.GONE);
            data.setSelect(false);
        }
        if (fileName.toLowerCase().endsWith("png") || fileName.toLowerCase().endsWith("jpg") || fileName.toLowerCase().endsWith("jpeg")) {
            holder.item_center_iv.setVisibility(View.GONE);
            if (list_bean.get(position).getCachePath() != null) {
                Glide.with(context).load(data.getCachePath()).placeholder(R.mipmap.loading).into(holder.item_iv);
                holder.item_name.setText(data.getFileName());
            } else {
                holder.item_iv.setImageResource(R.mipmap.waitload);
            }
        } else if(fileName.toLowerCase().endsWith("mp4")){
            holder.item_center_iv.setVisibility(View.VISIBLE);
            holder.item_center_iv.setImageResource(R.mipmap.video_play);
            if(data.getTime()!=null){
                holder.item_time.setVisibility(View.VISIBLE);
                holder.item_time.setText(data.getTime());
            }else{
                holder.item_time.setVisibility(View.GONE);
            }
            LogUtils.i("LocalRefreshAdapter thumbPath = "+data.getThumbPath());
            if(list_bean.get(position).getThumbPath()!=null){
                Glide.with(context).load(data.getThumbPath()).placeholder(R.mipmap.loading).into(holder.item_iv);
            }else{
                Glide.with(context).load(data.getCachePath()).placeholder(R.mipmap.loading).error(R.mipmap.waitload).into(holder.item_iv);
            }
            holder.item_name.setText(data.getFileName());
        } else{
            holder.item_center_iv.setVisibility(View.GONE);
            String name = data.getFileName();
            if(name.equals("IWT-AGS58")){
                holder.item_iv.setImageResource(R.mipmap.wenjianjia11);
                holder.item_iv.setScaleType(ImageView.ScaleType.CENTER);
                holder.item_name.setText(name);
                holder.item_time.setVisibility(View.GONE);
            }else if(name.equals("DCAM-H")){
                holder.item_iv.setImageResource(R.mipmap.wenjianjia11);
                holder.item_iv.setScaleType(ImageView.ScaleType.CENTER);
                holder.item_name.setText(name);
                holder.item_time.setVisibility(View.GONE);
            }
        }

    }

    class LocalHolder extends BaseRecyclerAdapter.BaseRecyclerViewHolder{
        private int position;
        private ImageView item_iv;
        private ImageView item_center_iv,select_iv;
        private TextView item_name,item_time;
        public LocalHolder(View itemView) {
            super(itemView);
            item_iv = itemView.findViewById(R.id.item_iv);
            item_center_iv = itemView.findViewById(R.id.item_center_iv);
            select_iv = itemView.findViewById(R.id.select_iv);
            item_name = itemView.findViewById(R.id.item_name);
            item_time = itemView.findViewById(R.id.item_time);

        }


    }
}
